package serverManager;

import java.io.IOException;

import org.mortbay.jetty.Server;
import org.mortbay.jetty.servlet.Context;
import org.mortbay.jetty.servlet.ServletHolder;

import webInterface.ContentOfDir;
import webInterface.Login;
import webInterface.LoginValidation;
import webInterface.SharingDir;
import webInterface.UserToShare;

/**
 * this class is the class that holds the main for this project
 */
public class ServerManager
{	
	/**
	 * this class is the main class of this project
	 * @param args
	 * @throws IOException 
	 * @throws ClassNotFoundException
	 */
	public static void main(String[] args) throws Exception
	{
		// Initializing the threads
		UploadFile uf = new UploadFile();
		UpdateUser uu = new UpdateUser();
		UpdateDir ud = new UpdateDir();
        Server server = new Server(8080); 
        Context root = new Context(server,"/",Context.SESSIONS);
        // connecting urls with handlers
        root.addServlet(new ServletHolder(new Login()), "/*");
        root.addServlet(new ServletHolder(new LoginValidation()), "/webInterface.LoginValidation");
        root.addServlet(new ServletHolder(new ContentOfDir()), "/webInterface.ContentOfDir");
        root.addServlet(new ServletHolder(new ConnectionWithSpy()), "/serverManager.ConnectionWithSpy");
        root.addServlet(new ServletHolder(new SharingDir()), "/webInterface.SharingDir");
        root.addServlet(new ServletHolder(new UserToShare()), "/webInterface.UserToShare");
        // running the threads
		uf.start();
		uu.start();
		ud.start();
        server.start();
        // waiting for the threads to finish - this project runs forever (because it's a server)
		try
		{
			uf.join();
			uu.join();
			ud.join();
			server.join();
		}
		// the case that something is wrong with one of the threads 
		catch(Exception e)
		{
			uf.shutDownServer();
			uu.shutDownServer();
			ud.shutDownServer();
		}
	}

}
